package com.bolt.support.query.nativequery.parall;

import com.bolt.common.collection.IteratorUtil;
import com.bolt.convention.exception.DataAccessException;

import java.io.Serializable;
import java.util.List;
import java.util.Objects;

public class ParallelQueryResult implements Serializable {

    private Class returnClass;

    private List rows;

    public Class getReturnClass() {
        return returnClass;
    }

    public void setReturnClass(Class returnClass) {
        this.returnClass = returnClass;
    }

    public List getRows() {
        return rows;
    }

    public void setRows(List rows) {
        this.rows = rows;
    }

    /**
     * 是否成功
     */
    private boolean success = true;

    /**
     * 执行信息
     */
    private String message;


    public boolean isSuccess() {
        return success;
    }

    public void setSuccess(boolean success) {
        this.success = success;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public <T> List<T> unwrap(Class<T> clazz) {
        if (Objects.isNull(rows)) {
            return rows;
        }
        if (returnClass.isAssignableFrom(clazz)) {
            return (List<T>) rows;
        } else {
            throw new DataAccessException("Could not unwrap this query result as requested Java type [" + clazz.getName() + "]");
        }
    }

    public <T> T unwrapOne(Class<T> clazz) {
        List<T> datas = unwrap(clazz);
        if (IteratorUtil.isNotEmpty(datas)) {
            return datas.get(0);
        } else {
            return null;
        }
    }
}